/**

 @Name：layuiAdmin 用户管理 用户列表
 @Author：xsh
 
 @License：LPPL
    
 */


layui.define(['table', 'form', 'layTable', 'dropdown'], function (exports) {
  var $ = layui.$
    , admin = layui.admin
    , view = layui.view
    , table = layui.table
    , setter = layui.setter
    , form = layui.form
    , dropdown = layui.dropdown //下拉菜单
    , layTable = layui.layTable;

  var cols = [[
    { type: 'checkbox', fixed: 'left' }
    , { field: 'id', width: 50, title: 'ID', sort: true }
    , {
      field: "name",
      width: 400,
      title: "角色名",
      align: "center"
    },
    {
      field: "orderNo",
      width: 160,
      title: "排序",
      align: "center"
    }
    , { title: '操作', width: 300, align: 'center', fixed: 'right', toolbar: '#table-toolbar' }
  ]];

  righttool = ['filter', 'print', { //'exports',
    //自定义头部工具栏右侧图标。如无需自定义，去除该参数即可
    title: '提示'
    , layEvent: 'LAYTABLE_TIPS'
    , icon: 'layui-icon-tips'
  }];

  // 创建表格   表格id  表格列  请求地址  查询formid
  layTable.CreateTable("LAY-userlist-tableid", cols, "/role-api/role-list-by-page", "lay-userlist-search-view", "#my-table-tool", righttool, "");

  //监听工具条 table toolsbar 右侧记录菜单按钮事件 lay-event == obj.event
  //tool(lay-filter name)
  table.on('tool(LAY-userlist-table)', function (obj) {
    var data = obj.data;
    // console.info(obj);
    if (obj.event === 'edit') {
      // console.info(data);
      insertUpdateObj(data);
    } else if (obj.event === 'menuset') {
      layer.msg('menuset');
    } else if (obj.event === 'del') {
      delObjs([data]);
    }

  });

  var insertUpdateObj = function (obj) {
    admin.popup({
      title: '添加|编辑角色'
      , area: ['420px', '280px']
      , id: 'LAY-popup-userlist-add'
      , success: function (layero, index) {
        //view.render('模板路径','数据').done....
        //提交 回调模板内的时间

        var _data = obj;

        view(this.id).render('xrole/popup/add', _data).done(function () {

          //lay-filter
          form.render(null, 'layui-form-add-user-page');

          form.on('submit(LAY-user-add-submit)', function (form) {
            var field = form.field; //获取提交的字段
            console.info(field);

            admin.req({
              url: setter.apiUrl + '/role-api/role',
              type: 'POST',
              data: JSON.stringify(field),
              contentType: "application/json; charset=utf-8",
              xhrFields: {
                withCredentials: true  // 如果是https请求，可以试试 true
              },
              success: function (result) {
                console.info(result);
                table.reload('LAY-userlist-tableid');
                layer.msg('已完成');
              },
              error: function (e) {
                layer.msg("操作失败！！！\n" + e);
              }
            });

            layui.table.reload('LAY-userlist-tableid'); //重载表格
            layer.close(index); //执行关闭 
          });
        });
      }
    });
  }

  var delObjs = function (objs) {
    layer.prompt({
      formType: 1
      , title: '敏感操作，请验证口令'
    }, function (value, index) {

      if (value != "admin") {
        layer.msg('口令错误，操作失败！！！');
        layer.close(index);
        return false;
      }

      layer.close(index);

      layer.confirm('确定删除吗？', function (index) {
        admin.req({
          url: setter.apiUrl + '/role-api/roles',
          type: 'DELETE',
          data: JSON.stringify(objs),
          contentType: "application/json; charset=utf-8",
          xhrFields: {
            withCredentials: true  // 如果是https请求，可以试试 true
          },
          success: function (result) {
            console.info(result);
            table.reload('LAY-userlist-tableid');
            layer.msg('已删除');
          },
          error: function (e) {
            layer.msg("删除失败！！！\n" + e);
          }
        });

      });
    });
  }

  //table 头部自定义左侧区域 包括右上角自定义 按钮事件 lay-event == obj.event
  //toolbar(lay-filter name)
  table.on('toolbar(LAY-userlist-table)', function (obj) {
    var checkStatus = table.checkStatus(obj.config.id);
    // console.info(obj);
    switch (obj.event) {
      case 'adduser':
        insertUpdateObj({ name: "新角色名称", deleteFlag: "false", orderNo: "999", id: "0" });
        break;
      case 'multipledel':
        var checkStatus = table.checkStatus('LAY-userlist-tableid')
          , checkData = checkStatus.data; //得到选中的数据

        if (checkData.length === 0) {
          return layer.msg('请选择数据');
        }

        delObjs(checkData);

        break;
      case 'LAYTABLE_TIPS':
        layer.msg('Hi，头部工具栏扩展的右侧图标。');
    };
  });

  exports('xrolelist', {})
});